import * as api from '../api/api';
import request from '../utils/request';
import { pageSize } from '../utils/common';
import queryString from 'query-string';

export default {
    namespace: 'refund',
    state: {
        refund: {},
        applies: { totalCount: 0, items: [] },
        applySearchs: { beginTime: '', endTime: '', pageIndex: 1, pageSize: pageSize },
        histories: { totalCount: 0, items: [] },
        historySearchs: { beginTime: '', endTime: '', pageIndex: 1, pageSize: pageSize },
    },
    effects: {
        /**
         * 获取退款申请列表
         * @param param0 
         * @param param1 
         */
        *getRefundApplies({ payload }, { call, put }) {
            const { beginTime, endTime, pageIndex, pageSize } = payload;
            const formBeginTime = beginTime,
                formEndTime = endTime,
                formMaxResultCount = pageSize,
                formSkipCount = (pageIndex - 1) * pageSize;
            const applies =
                yield request(api.ManagerefundManageApiFetchParamCreactor.managerefundManageGetRefundAppliesAsync(
                    { formBeginTime, formEndTime, formMaxResultCount, formSkipCount }
                ))
            yield put({ type: 'save', payload: { applies, applySearchs: { beginTime, endTime, pageIndex, pageSize } } });
        },
        /**
         * 获取退款历史列表
         * @param param0 
         * @param param1 
         */
        *getRefundHistories({ payload }, { call, put }) {
            const { beginTime, endTime, pageIndex, pageSize, status } = payload;
            const formBeginTime = beginTime,
                formEndTime = endTime,
                formMaxResultCount = pageSize,
                formSkipCount = (pageIndex - 1) * pageSize,
                formStatus = status;
            const histories =
                yield request(api.ManagerefundManageApiFetchParamCreactor.managerefundManageGetRefundHistoriesAsync(
                    { formStatus,formBeginTime, formEndTime, formMaxResultCount, formSkipCount }
                ))
            yield put({ type: 'save', payload: { histories, historySearchs: { beginTime, endTime, pageIndex, pageSize } } });
        },
        /**
         * 获取退款详情
         * @param param0 
         * @param param1 
         */
        *getRefund({ payload }, { call, put }) {
            const { id } = payload;
            const formId = id;
            const refund =
                yield request(api.ManagerefundManageApiFetchParamCreactor.managerefundManageGetRefundAsync(
                    { formId }
                ))
            yield put({ type: 'save', payload: { refund } });
        },
        /**
         * 驳回退款
         * @param param0 
         * @param param1 
         */
        *reject({ payload }, { call, put }) {
            const { form, callback } = payload;
            yield request(api.ManagerefundManageApiFetchParamCreactor.managerefundManageRejectAsync(
                { form }
            ));
            callback && callback(true);
        },
        /**
         * 退款通过
         * @param param0 
         * @param param1 
         */
        *pass({ payload }, { call, put }) {
            const { form, callback } = payload;
            yield request(api.ManagerefundManageApiFetchParamCreactor.managerefundManagePassAsync(
                { form }
            ));
            callback && callback(true);
        },
    },
    reducers: {
        save(state, action) {
            return { ...state, ...action.payload };
        },
    },
    subscriptions: {
        setup({ dispatch, history }) {
            return history.listen(({ pathname, search }) => {
                switch (pathname) {
                    case '/order/refund':
                        const { id } = queryString.parse(search);
                        if (id) {
                            dispatch({ type: 'getRefund', payload: { id } });
                        }
                        else {
                            dispatch({ type: 'save', payload: { refund: {} } });
                        }
                        break;
                }
            });
        },
    },
};